package CtrlLayer;
import java.util.ArrayList;
import ModelLayer.*;

public class SaleController
{
    private SaleContainer saleContainer;
    private CustomerController customerController;
    private ProductController productController;
    private OrderController orderController;

    public SaleController()
    {
        saleContainer = SaleContainer.getInstance();
        customerController = new CustomerController();
        productController = new ProductController();
        orderController = new OrderController();
    }

  
    public int createSale(int customerID,int sessionId){  
        int employeeID = SessionContainer.getInstance().findSessionById(sessionId).getPersonId();
        Sale sObject = null;
        int index = 0;
        int id = 0;
        while(index < saleContainer.getSaleList().size())
        {
            sObject = saleContainer.getSaleList().get(index);
            if(sObject !=null){
                if(sObject.getId()>id)   
                    id = sObject.getId();
            }else{
                id = 1;
            }
            index++;
        }
        id++;
        if(CustomerContainer.getInstance().checkCustomer(customerID)){
            saleContainer.addSale(new Sale(id, employeeID, customerID));
            return id;
        } else {
            return 0;
        }
    }

    public boolean endSale(int id, int sessionId){
        saleContainer.findSaleById(id).setReady();
        if(saleContainer.findSaleById(id).isReady()) {
            for (int key : saleContainer.findSaleById(id).getProducts().keySet()){
                if(ProductContainer.getInstance().findProductByBarcode(key).getAmount() + ProductContainer.getInstance().findProductByBarcode(key).getOrderAmount()  < ProductContainer.getInstance().findProductByBarcode(key).getMaxAmount())
                    orderController.createOrderAutomatically(id, sessionId);
            }
            return true;
        } else {
            return false;
        }
    }

    public boolean addProductInSale(int id, int productBarcode, int amount){
        if(!saleContainer.findSaleById(id).isReady() && productController.findProduct(productBarcode) != null) {
            saleContainer.findSaleById(id).addProduct(productBarcode,amount);
            productController.findProduct(productBarcode).recalculateAmount(amount);
            return true;
        } else {
            return false;
        }
    }

    public boolean removeProductFromSale(int id, int productBarcode,int amount){
        if(! saleContainer.findSaleById(id).isReady() && productController.findProduct(productBarcode) != null) {
            saleContainer.findSaleById(id).removeProduct(productBarcode,amount);
            productController.findProduct(productBarcode).recalculateAmount(-amount);
            return true;
        } else {
            return false;
        }
    }

    public Sale findSale(int id){    
        return  saleContainer.findSaleById(id);
    }

    public void deleteSale(int id){
        saleContainer.deleteSale(saleContainer.findSaleById(id));
    }

    public void updateSale(int id){
        Sale s =  saleContainer.findSaleById(id);
        if(s != null){
            //    p.setBarcode(barcode);

        }
    }

}
